/**
 * Creates new Airpodder.util.FeedParser
 * @constructor
 * @param {Object} config A config object
 */
Airpodder.util.FeedParser = function() {
  this.addEvents(
    "feedloaded"
  );
  Airpodder.util.FeedParser.superclass.constructor.call(this);
};

Ext.extend(Airpodder.util.FeedParser, Ext.util.Observable, {
  //Конфигурация подкаст фидов
  type_feeds: {
    rss2_0: {
      channel_info: {
        title       : 'channel/title',
        link        : 'channel/link',
        description : 'channel/description',
        image       : ['channel/image/url', 'channel/itunes:image/@href'],
        pubDate     : 'channel/pubDate'
      },
      items_info  : {
        xpath       : 'channel/item',
        url         : ['enclosure/@url', 'media:content/@url'],
        title       : 'title',
        len         : ['enclosure/@length', 'media:content/@fileSize'],
        mimetype    : ['enclosure/@type', 'media:content/@type'],
        guid        : 'guid',
        description : ['content:encoded', 'description'],
        link        : 'link',
        pubDate     : 'pubDate'
      }
    }
  },

  checkFeedType: function (feed) {
    var result = {
      type    : 'rss',
      version : '2_0'
    }
    if (feed.nodeName.toLowerCase().indexOf('rss') != -1 ) {
      result.type     = 'rss';
      result.version  = root.getAttribute('version').replace('.', '_');
    }
    return result;
  },

  getFeed: function (feed_url) {
    Ext.Ajax.request({
      url     : feed_url,
      method  : 'GET',
      scope   : this,
      success : this._parseResult
    });
  },

  _parseResult: function(response, request) {
    try {
      var xml = response.responseXML;
      if (null == xml) {
        xml = new DOMParser().parseFromString(responseXML.responseText, "text/xml");
        if (false == xml || "parsererror" == xml.documentElement.nodeName) {
          return false;
        }
      }
      var feed_type = this.checkFeedType(xml);
      var sel       = this.type_feeds[feed_type.type + feed_type.version];
      var channel   = sel.channel_info;
      var episode   = sel.items_info;
      var res       = {
        title       : this._getNodeValue(channel.title,       xml),
        link        : this._getNodeValue(channel.link,        xml),
        description : this._getNodeValue(channel.description, xml),
        image       : this._getNodeValue(channel.image[0],    xml),
        pubDate     : this._getNodeValue(channel.pubDate,     xml),
        items       : []
      }
      var items = Ext.DomQuery.select(episode.xpath, xml);
      for (var i = 0; i < items.length; i++) {
        res.items[res.items.length++] = {
          title       : this._getNodeValue(episode.title,           items[i]),
          guid        : this._getNodeValue(episode.guid,            items[i]),
          link        : this._getNodeValue(episode.link,            items[i]),
          pubDate     : this._getNodeValue(episode.pubDate,         items[i]),
          url         : this._getItemAttribute(episode.url,         items[i]),
          len         : (Ext.isEmpty(this._getItemAttribute(episode.len, items[i])) ? 0 : this._getItemAttribute(episode.len, items[i])),
          mimetype    : this._getItemAttribute(episode.mimetype,    items[i]),
          description : this._getItemAttribute(episode.description, items[i])
        };
        
      }
      return this.fireEvent('feedloaded', request.url, res);
    } catch (e) {
      c_log(e);
    }
  },
  _getItemAttribute: function(xpath, xml) {
    var value = '';
    for (var j = 0; j < xpath.length; j++) {
      if (Ext.isEmpty(value) == false) {
        continue;
      }
      value = this._getNodeValue(xpath[j], xml);
    }
    return value;    
  },

  _getNodeValue: function(xpath, xml) {
    try {
      var value = Ext.DomQuery.select(xpath, xml)[0].firstChild.nodeValue;
    } catch (e) {
      var value = '';
    }
    return value;
  }
});
